
$(function () {
  if ($("#msgError").html() != "") {
	  $("#msgError").show();
  }
  
  $("#login-btn").bind("click", function () {
	  x.login("#form-login","#login-btn","/login/basics")
  });
  $(".form-txt").bind("keypress", function (e) {
	  if(e.keyCode == "13")      
      {  
		  $("#login-btn").trigger("click");
      }  
	 
  });

  $(".form-txt").bind("input propertychange", function (e) {
	  $("#msgError").hide().empty();
	  if ($("#userName").val().length>0&&$("#userPassword").val().length>0&&$("#userImgCode").val().length>0) {
		  
			$("#login-btn").removeAttr("disabled")
	}else{
		$("#login-btn").attr("disabled","disabled")
	}
  });
  //验证码
  $(".imgCode-change").bind("click", function () {
    $(".verifyCode").attr('src', x.imgCodeUrl($(".verifyCode").attr('src'))); return false;
  });
store.session.remove('basicsMenu');
store.session.remove('basicsUserInfo');
$.removeCookie('SideMenuCur', { path: '/' });

	
});

x.login = function (formid,btn,url) {
	 if (!$(formid).valid()) {
		 $(".form-txt").removeAttr("disabled");
	    return false;
	  }
	 $(".form-txt").attr("disabled","disabled")
	  var userName = $('#userName').val();
	  var userPassword = $('#userPassword').val();
	  var userImgCode = $('#userImgCode').val();
	  var data = {
	    'loginName': userName,
	    'password': dataCrypt(userPassword),
	    'imageCode': userImgCode
	  };
	  $.ajax({
	    url: "/login",
	    type: "post",
	    dataType: "json",
	    data: data,
	    beforeSend:function(){
	    	$(btn).attr("disabled","disabled").html($(btn).attr("data-loading"))
	    },
	    success: function (res) {
	      if(res.code!=0){
	    	  $("#msgError").show().empty().html(res.msg);
	    	  $(btn).addClass("btn-login-disabled").html($(btn).attr("data-title"))
	    	  $('#userImgCode').val("");
	    	  $(".imgCode-change").trigger("click");
	    	  $(".form-txt").removeAttr("disabled");
	    	  return false;
	      }else{
	    	  window.location.href="/portal/index"; 
	      }
	    },
	    complete:function(){
	    	//$(btn).removeAttr("disabled").html($(btn).attr("data-title"))
	    }
	  });
}

x.imgCodeUrl = function (url) {
  var timestamp = (new Date()).valueOf();
  if ((url.indexOf("?") >= 0)) {
    url = url + "&tamp=" + timestamp;
  } else {
    url = url + "?timestamp=" + timestamp;
  }
  return url;
};